package com.lanou.zy.projecta.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.lanou.zy.projecta.R;
import com.lanou.zy.projecta.adapter.QuestionRecyclerViewAdapter;
import com.lanou.zy.projecta.entity.Question;

import java.util.Date;
import java.util.List;

/**
 * Created by dllo on 15/11/3.
 */
public class QuestionFragment extends Fragment{
    private RecyclerView questionRecyclerView;
    private QuestionRecyclerViewAdapter questionRecyclerViewAdapter;
    private View view;
    private TextView questionIn_Tv;
    private RequestQueue queue;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_question, null);
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
    }

    private void initView() {
        questionRecyclerViewAdapter = new QuestionRecyclerViewAdapter();
        questionRecyclerView = (RecyclerView) view.findViewById(R.id.questionRecyclerView);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 1);
        gridLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        questionRecyclerView.setLayoutManager(gridLayoutManager);
        questionRecyclerView.setAdapter(questionRecyclerViewAdapter);
    }

    private void initData() {
        queue = Volley.newRequestQueue(getActivity());
        long str = new Date().getTime();
        String url = "http://open.qyer.com/qyer/ask/question/get_new?client_id=qyer_android&client_secret=9fcaae8aefc4f9ac4915&v=1&track_deviceid=863811018458956&track_app_version=6.8.1&track_app_channel=taobao&track_device_info=msm8960&track_os=Android4.2.2&app_installtime=" + str + "&lat=38.883641&lon=121.544679&limit=10";
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                Question q = gson.fromJson(response, Question.class);
                List<Question.DataEntity> qd = q.getData();
                questionRecyclerViewAdapter.addData(qd);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });
        queue.add(request);
    }
}
